// Save the min_max for the see more places button
var cur_lat, cur_lng = '';

$(function () {
    $('#search-places-info .num-result').hide();
    $('#search-places-info .no-result').hide();
    
    $('#btn-search-place').click(search_karaoke_places);
    $('#btn-find-direction').click(find_direction);
    $('#more-karaoke-place span.text').click(get_more_places);
    
    // Show hide map in the mobile
    $('.show-hide-map').click(show_hide_map);
});

function search_karaoke_places() {
    // Change the action for the more button
    $('#more-places-btn').attr('action', '/index/get_karaoke_places');
    
    var address = $('#search-place').val();
    search_address_gmap(address, {w: 18, h:28}, function (res) {
        if (res.success) {
            get_karaoke_places(res.lat, res.lng)
        }
        
        // Get address
        get_address(res.lat, res.lng, function (res) {
            $('#current-address').html(res.address);
        });
    })
}

function get_karaoke_places(cur_lat, cur_lng, not_overide_data, is_more_data) {
    
    // Keep the location
    set_current_latlng(cur_lat, cur_lng)
    
    // Handle get more places button
    var num_displayed = 0;
    if (is_more_data) {
        num_displayed = $('#karaoke-places-list .item').length;
    };
    
    $.ajax({
        type: 'post',
        url: $('#more-places-btn').attr('action'),
        dataType: 'json',
        data: { lat: cur_lat, 
                lng: cur_lng,
                num_displayed: num_displayed
            },
        beforeSend: function () {
            $('#btn-search-place').html('Đang tìm');
            $('#btn-search-place').prop('disabled', true);
            
            if (is_more_data) {
                $('#more-karaoke-place span.text').html('Đang tìm ...');
                $('#more-karaoke-place span.text').prop('disabled', true);
            }
        },    
        success: function (res) {
            $('#btn-search-place').html('Tìm kiếm');
            $('#btn-search-place').prop('disabled', false);
            var total_displayed = num_displayed + 15;
            if (is_more_data) {
                $('#more-karaoke-place span.text').html('Xem thêm kết quả ...');
                $('#more-karaoke-place span.num').html('('+ total_displayed +'/' + res.total +')');
                $('#more-karaoke-place span.text').prop('disabled', false);
            }
            
            if (res && ! res.html && not_overide_data) {
                return
            } 
            
            // Set total places
            if (res.current_location && res.total) {
                $('#search-places-info .num-result span').html(res.total);
                $('#search-places-info .num-result').show();
                $('#search-places-info .no-result').hide();
            } else {
                $('#search-places-info .num-result').hide();
                $('#search-places-info .no-result').show();
            }
            
            if (! is_more_data) {
                $('#karaoke-places-list').html(res.html);
            } else {
                var more_list = 'more-list-'+num_displayed+'';
                
                $('#karaoke-places-list').append('<div id="'+ more_list +'"></div>');
                $('#'+ more_list).hide();
                $('#'+ more_list).html(res.html);
                $('#'+ more_list).slideDown('slow');
                
                $('.last-item').removeClass('no-border');
            }
            
            // Hide see more button if not enought data
            if (! res.see_more) {
                $('#more-places-btn').hide();
            } else {
                $('#more-places-btn').show();
            }
            
            set_places_marker(res.places, map);
        }
    })
}


function find_direction() {
    calc_route($('#from-place').val(), $('#to-place').val());
    
    $('.distance .km').html('...');
    
    geocoder.geocode({address: $('#from-place').val()}, function(results, status) {

        if (status == google.maps.GeocoderStatus.OK) {
            var first_result    = results[0],
               location        = first_result.geometry.location;
            
            var km = measure($('#detail-latitude').val(), $('#detail-longitude').val(), location.lat(), location.lng());   
            $('.distance .km').html(km+ ' km');
        }
    });    
}

function get_more_places() {
    get_karaoke_places(cur_lat, cur_lng, false, true);
}

function show_hide_map() {
    $('#map_canvas').toggle('hidden');   
}

function set_current_latlng(_cur_lat, _cur_lng) {
    cur_lat = _cur_lat;
    cur_lng = _cur_lng;
}